from src.general_function import TestCase, TestSuite, AllTest, List, get_all_utest_dirs
import os
import json
import pickle


def create_case(name: str, result: str, time: int, classname: str, status: str):
    """
    Функция для создания экземпляров класса TestCase
    :return: TestCase
    """
    return TestCase(name=name, result=result, time=time, classname=classname, status=status)


def create_suite(name: str, tests: int, failures: int, disabled: int, time: int, errors: int, cs):
    """
    Функция для создания экземпляров класса TestSuite
    :return: TestSuite
    """
    return TestSuite(name=name, tests=tests, failures=failures, disabled=disabled, time=time, errors=errors,
                     testsuite=[create_case(c['name'], c['result'], c['time'], c['classname'], c['status']) for c in cs])


def create_all(name: str, tests: int, failures: int, disabled: int, time: int, errors: int, bs: List):
    """
    Функция для создания экземпляров класса AllTest
    :return: AllSuite
    """
    return AllTest(name=name, tests=tests, failures=failures, disabled=disabled, time=time, errors=errors,
                   testsuites=[create_suite(b['name'], b['tests'], b['failures'], b['disabled'],
                                            b['time'], b['errors'], b['testsuite']) for b in bs])


def json_in_class(list_json):
    """
    Функция для преобразования споска путей в список классов
    :param list_json: список путей ло results.json
    :return: спосок классов
    """
    total_test = []
    for i_file in list_json:
        with open(os.path.join(i_file, "results.json"), "r") as file_json:
            data = json.load(file_json)
            total_test.append(create_all(data['name'], data['tests'], data['failures'], data['disabled'],
                              data['time'], data['errors'], data['testsuites']))
    return total_test

# def json_in_class(list_json):
#     """
#     Функция для преобразования споска путей в список классов
#     :param list_json: список путей ло results.json
#     :return: спосок классов
#     """
#     total_test = []
#     for i_file in list_json:
#         with open(os.path.join(i_file, "results.json"), "r") as file_json:
#             data = json.load(file_json)
#             total_test = [(data['name'], create_case(j['name'], j['result'], j['time'], j['classname'],
#                               j['status'])) for i in data['testsuites'] for j in i['testsuite']]
#     return total_test

if __name__ == "__main__":
    file = r"C:\Users\anyca\Desktop\TestResults_Unpack"
    json_list = json_in_class(get_all_utest_dirs(file))
    with open('class_list.pkl', 'wb') as file:
        pickle.dump(json_list, file)
